/*
**
**			Heroes IV
**	  Copyright 2001, The 3DO Company
**
**      dialog_magic_gem.cpp
*/
// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "dialog_magic_gem.h"

#include "adaptor_handler.h"
#include "army.h"
#include "basic_dialog.h"
#include "bitmap_group.h"
#include "bitmap_group_cache.h"
#include "bitmap_group_window.h"
#include "bitmap_layer.h"
#include "bitmap_layer_window.h"
#include "bound_handler.h"
#include "button.h"
#include "button_cache.h"
#include "creature_icon_window.h"
#include "creature_select_window.h"
#include "external_string.h"
#include "format_string.h"
#include "game_replace_keywords.h"
#include "hero.h"
#include "interface_help.h"
#include "player.h"
#include "random.h"
#include "replace_keywords.h"
#include "skill.h"
#include "skill_button.h"
#include "skill_icon.h"
#include "skill_properties.h"
#include "standard_fonts.h"
#include "stationary_adventure_object.h"
#include "text_window.h"

extern  t_bitmap_group_cache k_pick_hero_bitmaps;
extern  t_button_cache g_ok_button;
extern  t_button_cache g_cancel_button;
extern  t_bitmap_group_cache k_stat_icons_82;

// *** NOTE - using placeholder art!  (dialog.pick_hero) - AH

// Add to max spell points. If possible, also add to current spell points
void add_to_spell_points( t_hero* hero, int amount )
{
    hero->add_bonus( k_stat_spell_points, amount );

	int spell_points = hero->get_spell_points();
	int new_spell_points = spell_points + amount;
	int maximum_points = hero->get_maximum_spell_points();

	if ( spell_points < maximum_points )
	{
		if ( new_spell_points > maximum_points )
			new_spell_points = maximum_points;
		hero->set_spell_points( new_spell_points );
	}
}

void give_hero_gem_bonus( t_hero* hero, t_magic_gem_type type, t_basic_dialog* result_dialog )
{
    switch  (type)
    {
        case k_gem_opal_of_magic :
		{
			add_to_spell_points( hero, 6 );
			if (result_dialog != NULL)
				result_dialog->add_spell_points( 6 );
            break;
		}
        
        case k_gem_sapphire_of_health :
            hero->add_bonus( k_stat_defense, 6 );
			if (result_dialog != NULL)
		        result_dialog->add_defense( 6 );
            break;
        
        case k_gem_ruby_of_offense :
	        hero->add_bonus( k_stat_attack, 6 );
			if (result_dialog != NULL)
		        result_dialog->add_attack( 3 );
            break;
        
        case k_gem_emerald_of_speed :
            hero->add_bonus( k_stat_speed, 2 );
			if (result_dialog != NULL)
		        result_dialog->add_speed( 2 );
            break;
    }
}

/*
** Magic gem dialog constructor.  Choose hero for upgrade.
*/
t_dialog_magic_gem::t_dialog_magic_gem ( t_window* parent )
					: t_window( k_completely_transparent, parent )
{
}

void t_dialog_magic_gem::init_dialog( std::vector<t_hero*> const& heroes, t_magic_gem_type gem_type, t_stationary_adventure_object &parent_object )
{

	t_bitmap_layer const*   layer;
	t_window *              window;
	t_screen_point          origin(0,0);
	t_screen_point          point(0,0);
	t_screen_rect           rect;
    int                     i;

    m_gem_type = gem_type;
    
	m_bitmaps_ptr = k_pick_hero_bitmaps.get();
	layer = m_bitmaps_ptr->find( "background" );
	window = new t_bitmap_layer_window( layer, origin, this );

	point = m_bitmaps_ptr->find("hero_rings")->get_rect().top_left() + origin;

	m_hero_selection_window = new t_creature_toggle_window( point, this );

	for (i = 0; i < heroes.size(); i++)
	{
		m_hero_selection_window->add( heroes[i] );

	    m_hero_selection_window->enable( i, true );
	    m_hero_selection_window->set_help_text( i, heroes[i]->get_name() );
    }
	m_hero_selection_window->set_handler( bound_handler( *this, &t_dialog_magic_gem::hero_selection_change ));
	m_hero_selection_window->select_none();
	m_hero_selection_window->select_first();

	t_screen_rect ring_rect = m_bitmaps_ptr->find("hero_rings")->get_rect();
    int width = m_hero_selection_window->get_width();
    int offset_x = ((ring_rect.width()/2) - (width/2));
    
	//m_hero_selection_window->move( t_screen_point( offset_x, 0 ));
	m_hero_selection_window->move( ring_rect.top_left() + t_screen_point( offset_x, 0 ) );
                    
    m_selected_index = 0;
    hero_selection_change( NULL, NULL );

    /*
    ** Text windows.
    */
	t_text_window* text_window_ptr;

	rect = m_bitmaps_ptr->find( "title" )->get_rect() + origin;
	text_window_ptr = new t_text_window( get_font( 18 ), rect, this, parent_object.get_text("name"), t_pixel_24(0,0,0));
	text_window_ptr->set_center_horizontal();
	text_window_ptr->set_drop_shadow( false );

	/*
    ** Add the intro text.
    */
	m_intro_text = parent_object.get_text("initial");
	rect = m_bitmaps_ptr->find( "text" )->get_rect() + origin;
	text_window_ptr = new t_text_window( get_font( 18 ), rect, this, m_intro_text, t_pixel_24(0,0,0));
	text_window_ptr->set_center_horizontal();
	text_window_ptr->set_drop_shadow( false );

    /*
    ** Show bonus stat.
    */
	t_help_block const&	   shared_help = get_help_block( "shared" );
	t_bitmap_group_ptr     stat_bitmaps;
	std::string            stat_text;
	std::string            gain_text;

	stat_bitmaps = k_stat_icons_82.get();
	point = m_bitmaps_ptr->find("icon_frame")->get_rect().top_left() + origin;

    switch  (m_gem_type)
    {
        case k_gem_opal_of_magic :
	        stat_text = "spell_points";
            gain_text = "+6";
            break;
        
        case k_gem_sapphire_of_health :
	        stat_text = "melee_defense";
            gain_text = "+6";
            break;
        
        case k_gem_ruby_of_offense :
	        stat_text = "melee_attack";
            gain_text = "+6";
            break;
        
        case k_gem_emerald_of_speed :
	        stat_text = "speed";
            gain_text = "+2";
            break;
    }
	window = new t_bitmap_group_window( stat_bitmaps, point, stat_text, this );

	rect = m_bitmaps_ptr->find( "icon_text" )->get_rect() + origin;
	text_window_ptr = new t_text_window( get_font( rect.height() ), rect, this, gain_text, t_pixel_24(0,0,0));
	text_window_ptr->set_center_horizontal();
	text_window_ptr->set_center_vertical();
	text_window_ptr->set_drop_shadow( false );

    /*
    ** Create buttons.
    */
	t_button            *button_ptr;
	rect = m_bitmaps_ptr->find( "ok_button" )->get_rect();
	button_ptr = new t_button( g_ok_button.get(), rect.top_left(), this );
	button_ptr->set_click_handler( bound_handler( *this, &t_dialog_magic_gem::close_click ));
  set_help( button_ptr, shared_help, "accept" );

	rect = m_bitmaps_ptr->find( "cancel_button" )->get_rect();
	button_ptr = new t_button( g_cancel_button.get(), rect.top_left(), this );
	button_ptr->set_click_handler( bound_handler( *this, &t_dialog_magic_gem::cancel_click ));
  set_help( button_ptr, shared_help, "cancel" );
    
    /*
    ** Set the position and display the window.
    */
	t_screen_rect parent_rect = get_parent()->get_client_rect();
    
	rect = m_bitmaps_ptr->get_rect();
	rect += (parent_rect.size() - rect.size()) / 2;
	init( rect );
}

void t_dialog_magic_gem::hero_selection_change( t_creature_select_window*, t_creature_stack* )
{
	int i;

	for (i = 0; i < m_hero_selection_window->get_count(); i++)
    {
		if ((m_hero_selection_window->is_enabled(i)) && (m_hero_selection_window->is_selected(i)))
        {
            
		    m_hero_ptr = m_hero_selection_window->get_creature(i)->get_hero();
		    m_selected_index = i;
        }
    }
}

/*-------------------------------------------------------------------*\
** Clicked on the close button.
** Give hero bonus stat, show result.
\*-------------------------------------------------------------------*/
void t_dialog_magic_gem::close_click( t_button* button )
{
	t_counted_ptr<t_basic_dialog> result_dialog = new t_basic_dialog;
	std::string result_text = m_intro_text;

	give_hero_gem_bonus( m_hero_ptr, m_gem_type, result_dialog );

	//m_hero_ptr->check_level( frame );

#if (0)
    // Show result of hero getting bonus.
    result_dialog->set_text( result_text );
    result_dialog->add_ok_button();
    result_dialog->add_creature( (*m_hero_selection_window->get_creature(m_selected_index)), false );

    result_dialog->open(); // get_dialog_bottom( point ), true, k_align_bottom );
    result_dialog->run_modal();
#endif

	m_return_value = true;
	close();
}

void t_dialog_magic_gem::cancel_click( t_button* button )
{
	m_return_value = false;
	close();
}

